﻿using System;
using System.Data;
using System.Text;
using System.Collections.Generic;
using System.Linq;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Truextend.CoffeShop.Model;

namespace Truextend.Common.Data.Tests
{
    [TestClass]
    public class RestaurantsRepositoryTest
    {
        [TestMethod]
        public void SaveRestaurantTest()
        {
            var uow = new EFUnitOfWork<CoffeShopEntities>();
            uow.Start();
            Restaurant restaurant = new Restaurant()
            {
                Name = "Internal Product 2",
                Phone1 = "44454534",
                Address = "xcvxgdfgdgd"
            };
            var repository = new Repository<Restaurant>(uow);
            
            var previousRecords = repository.SelectAll().Count;
            repository.Add(restaurant);
            uow.End();
            var currentRecords = repository.SelectAll().Count;
            Assert.AreEqual(previousRecords + 1, currentRecords);
            Assert.AreEqual(ConnectionState.Closed, uow.Context.Connection.State);
        }

        [TestMethod]
        public void SaveRestaurantWithExceptionTest()
        {
            Restaurant restaurant = new Restaurant()
            {
                Name = "44454534",
                Address = "xcvxgdfgdgd"
            };
            int previousRecords = 0;
            int currentRecords = 0;
            var uow = new EFUnitOfWork<CoffeShopEntities>();
            var repository = new Repository<Restaurant>(uow);
            try
            {
                uow.Start();
                previousRecords = repository.SelectAll().Count;
                repository.Add(restaurant);
                throw new Exception("Induced Error");
                //currentRecords = repository.SelectAll().Count;
                uow.End();
            }
            catch(Exception)
            {
                uow.Rollback();
                currentRecords = repository.SelectAll().Count;
            }
            Assert.AreEqual(previousRecords, currentRecords);
            Assert.AreEqual(ConnectionState.Closed,uow.Context.Connection.State);

        }
    }
}
